﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTQT;
using TTQT.TTQT_Code;
using MyWebUtils;
using System.IO;

public partial class DonDatHang : MasterControl//System.Web.UI.UserControl
{
    //private void bindAnChi()
    //{
    //    dsMaAnChi.SelectCommandType = SqlDataSourceCommandType.Text;
    //    dsMaAnChi.SelectCommand =
    //        "select distinct Ma,tenanchi+ case(AnChiMau) when 1 then N' (mẫu)' when 0 then '' end [TenAnChi] from maanchi where MaDotPhatHanh=" +
    //        cboDotPhatHanh.SelectedValue;
    //    cboMaAnChi.DataSource = ((DataView)dsMaAnChi.Select(new DataSourceSelectArguments())).ToTable();
    //    cboMaAnChi.DataBind();
    //}

    SanPhamDAO sp = new SanPhamDAO();

    private void visiblePanels(bool grid,bool chiTiet)
    {
        pnGridDetail.Visible = grid;
        pnEditDetail.Visible = chiTiet;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            visiblePanels(true,false);
            bindDonHang("0");
        }
    }
    private void bindDonHang(string trangThai)
    {
        SanPham_DatHangDAO dao=new SanPham_DatHangDAO();
        grDonHang.DataSource = dao.getByTrangThai(trangThai);
        grDonHang.DataBind();
    }

    protected void cboTrangThai_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDonHang(cboTrangThai.SelectedValue);
    }
    protected void grDonHang_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if(e.CommandName.Equals("cmdEdit"))
        {
            SanPham_DatHangDAO dao = new SanPham_DatHangDAO();
            //dao.Id = e.Item.Cells[0].Text;
            dao.getById(e.Item.Cells[0].Text);
            if(dao.getId()>0)
            {
                visiblePanels(false,true);
                lblId.Text = dao.Id;
                lblNgayTao.Text = dao.NgayTao;
                lblHoTen.Text = dao.HoTen;
                lblHoTen1.Text = dao.HoTen1;
                lblDiaChi.Text = dao.DiaChi;
                lblDiaChi1.Text = dao.DiaChi1;
                lblEmail.Text = dao.Email;
                lblEmail1.Text = dao.Email1;
                lblDienThoai.Text = dao.DienThoai;
                lblDienThoai1.Text = dao.DienThoai1;
                lblDiDong.Text = dao.DiDong;
                lblDiDong1.Text = dao.DiDong1;
                lblGhiChu.Text = dao.GhiChu1;

                rptChiTiet.DataSource = dao.getChiTietDatHang(dao.Id);
                rptChiTiet.DataBind();

                lblTongTien.Text = TTQTCommon.formatTien_KhongThapPhan(dao.TongTien);
            }
        }
    }
    protected void cmdReturn_Click(object sender, EventArgs e)
    {
        visiblePanels(true,false);
    }
    protected void cmdCloseDon_Click(object sender, EventArgs e)
    {
        CommonDAO cd = new CommonDAO();
        cd.chayCauUpdate("update SanPham_DatHang set trangThai=1 where id=" + lblId.Text);
        bindDonHang("0");
        visiblePanels(true,false);
    }
    protected void cmdXoa_Click(object sender, EventArgs e)
    {
        CommonDAO cd = new CommonDAO();
        cd.chayCauUpdate("update SanPham_DatHang set trangThai=9 where id=" + lblId.Text);
        bindDonHang("0");
        visiblePanels(true, false);
    }
}
